﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>地图打印</title>
    <link  rel="stylesheet"  type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/dijit/themes/tundra/tundra.css"/>
    <link  rel="stylesheet"  type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/esri/css/esri.css" />
    <script  type="text/Javascript" src="http://localhost/arcgis_js_api/library/3.17/3.17/init.js"></script>
    <style type="text/css">
        .MapClass{
            width:100%;
            height:500px;
            border:1px solid #000;
        }
    </style>
    <script type="text/javascript">

        require(["esri/map",
			"dojo/dom","dojo/on","dojo/query",
            "esri/layers/ArcGISDynamicMapServiceLayer",
            "esri/symbols/SimpleMarkerSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/symbols/SimpleFillSymbol",
            "esri/toolbars/draw",
            "esri/graphic",
            "esri/tasks/PrintTask",
			"esri/tasks/PrintTemplate",
			"esri/tasks/PrintParameters",
			"dojo/colors",
            "dojo/domReady!"],
            function (Map,dom,on,query,
                ArcGISDynamicMapServiceLayer,
                SimpleMarkerSymbol,
                SimpleLineSymbol,
                SimpleFillSymbol,
                Draw,
                Graphic,
                PrintTask,PrintTemplate,PrintParameters,
                Color) {
                var map = new esri.Map("mapDiv");
                var layer = new esri.layers.ArcGISDynamicMapServiceLayer
                ("http://localhost:6080/arcgis/rest/services/Test/ft/MapServer");
                map.addLayer(layer);
				//创建绘图对象
                var toolBar = new Draw(map);	
                //线符号
                lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([255, 0, 0]), 3);
                //面符号
                polygonSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, lineSymbol, new Color([255, 0, 0, 0.25]));
				
				//给绘制polygon的按钮绑定事件
				query(".pbtn").on("click",function(event){
					//激活绘制多边形
					toolBar.activate(Draw.POLYGON, {
                        showTooltips:true
                    })
					
				})
				//绘图结束绑定事件
				on(toolBar,"draw-end",function(result){
					//获得面形状
					var geometry=result.geometry;
					//创建Graphic
					var graphicpoint= new Graphic(geometry, polygonSymbol);
                    map.graphics.add(graphicpoint); 
					//关闭绘图工具
                    toolBar.deactivate();					
				})
				//给地图打印按钮绑定事件
                on(dom.byId("Btn"),"click",function(){
					//创建地图打印对象
					var printMap = new PrintTask("http://localhost:6080/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task");
					//创建地图打印模版
					var template = new PrintTemplate();
					//创建地图的打印参数，参数里面包括：模版和地图
					var params = new PrintParameters(); 
					//输出图片的空间参考
					printMap.outSpatialReference = map.SpatialReference	
					//打印图片的各种参数
					template.exportOptions = { 
						width: 850, 
						height: 650, 
						dpi: 96 
					};
					//打印输出的格式
					template.format = "PDF"; 
					//输出地图的布局
					template.layout = "MAP_ONLY"; 
					PrintTemplate 
					//设置参数地图
					params.map = map; 
					//设置参数模版
					params.template = template; 
					//运行结果
					printMap.execute(params, function(result){
						if (result != null) { 
							//网页打开生成的地图
							window.open(result.url); 
						} 
					}) 
				})
               
            });
    </script>
</head>
<body>
    <div id="mapDiv" class="MapClass"></div>
	<button class="pbtn" >画多边形</button>
    <input id="Btn" type="button" value="地图打印" />
</body>
</html>
